using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

using TeleTwitter.Lib;

namespace WpfTeleTwitter
{
	internal class StatusList : ObservableCollection<Status>
	{
		private Status _mostRecentStatus;

		protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
		{
			base.OnCollectionChanged(e);

			if (e.Action == NotifyCollectionChangedAction.Add)
			{
				if (_mostRecentStatus == null)
					_mostRecentStatus = (Status) e.NewItems[0];

				foreach (Status status in e.NewItems)
				{
					if (status.CreatedAt > _mostRecentStatus.CreatedAt)
						_mostRecentStatus = status;
				}
			}
		}

		private ListStatus _listStatus = ListStatus.Uninitialized;
		
		public ListStatus ListStatus
		{
			get { return _listStatus; }
		}

		public Status MostRecentStatus
		{
			get { return _mostRecentStatus; }
		}

		public event EventHandler<ListStatusChangedEventArgs> ListStatusChanged;

		public void OnListStatusChanged(ListStatus listStatus)
		{
			if (_listStatus != listStatus)
			{
				_listStatus = listStatus;

				EventHandler<ListStatusChangedEventArgs> handler = ListStatusChanged;
				if (handler != null)
				{
					handler(this, new ListStatusChangedEventArgs(listStatus));
				}
			}
		}
	}

	internal class ListStatusChangedEventArgs : EventArgs
	{
		private readonly ListStatus _listStatus;
		
		public ListStatusChangedEventArgs(ListStatus listStatus)
		{
			_listStatus = listStatus;
		}

		public ListStatus ListStatus
		{
			get { return _listStatus; }
		}
	}

	internal enum ListStatus
	{
		Uninitialized,
		Initializing,
		Idle,
		Updating,
		Error,
	}
}
